﻿using Models;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;

namespace CommTestNFW.ImageCode
{

    /// <summary>
    /// 标签页对象
    /// </summary>
    public class CodePage
    {
        public CodePage(float width, float height)
        {
            Width = width;
            Height = height;
            Size = new SizeF(width, height);

            Padding = LabelConfig.RealPagePadding;

            CalulationPixel();
            Init();
        }
        /// <summary>
        /// 页面标识及序号
        /// </summary>
        public int Id { get; set; }

        /// <summary>
        /// 双排标识
        /// </summary>
        public bool DoublePage { get; set; }

        /// <summary>
        /// 生成后, 保存的image地址
        /// </summary>
        public string ImagePath { get; set; }
        public string OriginImagePath { get; set; }

        public SizeF Size { get; private set; }
        public float Width { get; set; }
        public float Height { get; set; }
        public float LabelWidth { get; set; }
        public float LabelHeight { get; set; }

        public int RowsCount { get; set; } = LabelConfig.RowsCount;//9
        public int ColsCount { get; set; } = LabelConfig.ColsCount;//24

        public Padding Padding { get; set; }
        public float Gap { get; set; }
        public float Pixel { get; private set; }


        /// <summary>
        /// 持有的标签
        /// </summary>
        public CodeLabel[] CodeLabels { get; private set; }


        /// <summary>
        /// 计算mm所占像素
        /// </summary>
        private void CalulationPixel()
        {
            int cols = ColsCount;
            //总宽 mm
            float mmSum = LabelConfig.RealWidth * cols + LabelConfig.RealGap * (cols - 1) + Padding.Horizontal;

            //每mm所占像素
            Pixel = Width / mmSum;

            LabelWidth = LabelConfig.RealWidth * Pixel;
            LabelHeight = LabelConfig.RealHeight * Pixel;

            //还原实际像素
            Padding.Boost(Pixel);

            Gap = LabelConfig.RealGap * Pixel;
        }

        public void SetCode(IEnumerable<PrintLabNo> codes)
        {
            var enumerator = codes.GetEnumerator();
            //enumerator.Reset();
            int i = 0, len = CodeLabels.Length;
            while (enumerator.MoveNext() && i < len)
            {
                CodeLabels[i++].PrintLabNo = enumerator.Current;
            }
        }

        /// <summary>
        /// 初始化标签列表
        /// </summary>
        private void Init()
        {
            var count = RowsCount * ColsCount;
            CodeLabels = new CodeLabel[count];

            for (int i = 0; i < RowsCount; i++)
            {
                System.Diagnostics.Debug.WriteLine(i);
                for (int j = 0; j < ColsCount; j++)
                {
                    //左上角x位置
                    float x = Padding.Left + (LabelWidth + Gap) * j;
                    //左上角y位置
                    float y = Padding.Top + (LabelHeight + Gap) * i;
                    PointF point = new PointF(x, y);

                    //第几个标签
                    int position = i * ColsCount + j;
                    System.Diagnostics.Debug.Write(position);

                    //设置标签内距
                    var label = new CodeLabel(point, LabelWidth, LabelHeight, Pixel);

                    CodeLabels[position] = label;
                }
                System.Diagnostics.Debug.WriteLine("");
            }
        }
    }
}
